package mmpathgen.callgraphanalysis;

import static org.junit.Assert.assertTrue;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;

public class CallGraphGenByCHATest {

	@Test
	public void testPattern() {
		// fail("Not yet implemented");

		Pattern p = Pattern.compile(".*a*b");
		Matcher m = p.matcher("baaab");
		boolean b = m.matches();

		assertTrue(b);
	}

	@Test
	public void testPrintPossibleCallees() {
		// fail("Not yet implemented");

		String[] pkgList = { "testers", };
		String[][] clsList = { { "CallGraphs", "A" }, };

		String mainClassName = "testers.CallGraphs";

		SootClass mainClass = Scene.v().loadClassAndSupport(mainClassName);

		SootMethod src = mainClass.getMethodByName("main");

		CallGraph cg = CallGraphGenByCHA.cgGen(pkgList, clsList, mainClassName);
		Targets tgts = CallGraphAnalysis.getPossibleCallees(cg, src);

		String tgts_out = new String();
		while (tgts.hasNext()) {
			SootMethod tgt = (SootMethod) tgts.next();
			tgts_out = tgts_out + tgt.toString();
		}
		System.out.println(tgts_out);

		// tgts_ora =
		// "<java.lang.Object: void <clinit>()><testers.CallGraphs: void doStuff()>"
		String tgt1_ora = new String("java.lang.Object: void <clinit>()");
		String tgt2_ora = new String("testers.CallGraphs: void doStuff()");
		String tgts_pattern_ora = ".*" + tgt1_ora + ".*" + tgt2_ora + ".*";
		System.out.println(tgts_pattern_ora);

		Pattern p = Pattern.compile(tgts_pattern_ora);
		Matcher m = p.matcher(tgts_out);
		boolean b = m.matches();

		assertTrue(b);
	}

}
